home *** CD-ROM | disk | FTP | other *** search
/ HaCKeRz Kr0nlcKLeZ 1 / HaCKeRz Kr0nlcKLeZ.iso / chibacity / gbbdisk.arj / ANTIVIR / GBSCAN.ASM < prev    next >
Encoding:
Assembly Source File  |  1995-06-16  |  16.8 KB  |  455 lines

  1. ;GB-SCAN Virus Scanner
  2. ;(C) 1995 American Eagle Publications, Inc., All Rights Reserved.
  3.  
  4. .model tiny
  5. .code
  6.  
  7. ;Equates
  8. DBUF_SIZE       EQU     16384           ;size of data buffer for scanning
  9.  
  10. ;These are the flags used to identify the scan strings and what they are for.
  11. BOOT_FLAG       EQU     00000001B       ;Flags a boot sector on the hard disk
  12. MBR_FLAG        EQU     00000010B       ;Flags a master boot sector on hd, boot sec on floppy
  13. EXE_FLAG        EQU     00000100B       ;Flags an EXE file
  14. COM_FLAG        EQU     00001000B       ;Flags a COM file
  15. RAM_FLAG        EQU     00010000B       ;Search RAM
  16. END_OF_LIST     EQU     00100000B       ;Flags end of scan string list
  17.  
  18.         ORG     100H
  19.  
  20. GBSCAN:
  21.         mov     ax,cs
  22.         mov     ds,ax
  23.  
  24.         mov     ah,19H                  ;get current drive number
  25.         int     21H
  26.         mov     BYTE PTR [CURR_DR],al   ;and save it here
  27.  
  28.         mov     ah,47H                  ;get current directory
  29.         mov     dl,0
  30.         mov     si,OFFSET CURR_DIR
  31.         int     21H
  32.  
  33.         mov     bx,5CH
  34.         mov     al,es:[bx]              ;get drive letter from FCB
  35.         or      al,al                   ;was one specified?
  36.         jnz     GBS1                    ;yes, go adjust as necessary
  37.         mov     ah,19H                  ;no, get current drive number
  38.         int     21H
  39.         inc     al
  40. GBS1:   dec     al                      ;adjust so A=0, B=1, etc.
  41.         mov     BYTE PTR [DISK_DR],al   ;save it here
  42.         mov     dl,al
  43.         mov     ah,0EH                  ;and make this drive current
  44.         int     21H
  45.  
  46.         push    cs
  47.         pop     es
  48.         mov     di,OFFSET PATH          ;set up path with drive letter
  49.         mov     al,[DISK_DR]
  50.         add     al,'A'
  51.         mov     ah,':'
  52.         stosw
  53.         mov     ax,'\'
  54.         stosw
  55.  
  56.         mov     dx,OFFSET HELLO         ;say "hello"
  57.         mov     ah,9
  58.         int     21H
  59.  
  60.         call    SCAN_RAM                ;is a virus in RAM?
  61.         jc      GBS4                    ;yes, exit now!
  62.         cmp     BYTE PTR [DISK_DR],2    ;is it drive C:?
  63.         jne     GBS2                    ;no, don't mess with master boot record
  64.         call    SCAN_MASTER_BOOT
  65. GBS2:   cmp     BYTE PTR [DISK_DR],2    ;is it drive D: or higher?
  66.         jg      GBS3                    ;yes, don't mess with boot sector
  67.         call    SCAN_BOOT
  68. GBS3:   mov     dx,OFFSET ROOT          ;go to root directory
  69.         mov     ah,3BH
  70.         int     21H
  71.         call    SCAN_ALL_FILES
  72.  
  73. GBS4:   mov     dl,[CURR_DR]            ;restore current drive
  74.         mov     ah,0EH
  75.         int     21H
  76.  
  77.         mov     dx,OFFSET CURR_DIR      ;restore current directory
  78.         mov     ah,3BH
  79.         int     21H
  80.  
  81.         mov     ax,4C00H                ;exit to DOS
  82.         int     21H
  83.  
  84.  
  85. ;This routine scans the Master Boot Sector.
  86. ;The drive to scan is supplied in dl.
  87. SCAN_MASTER_BOOT:
  88.         mov     WORD PTR [FILE_NAME],OFFSET MBR_NAME
  89.         push    ds                      ;first read the boot sector
  90.         pop     es
  91.         mov     bx,OFFSET DATA_BUF      ;into the DATA_BUF
  92.         mov     ax,201H
  93.         mov     cx,1
  94.         mov     dh,0
  95.         mov     dl,[DISK_DR]
  96.         cmp     dl,2
  97.         jc      SMB1
  98.         add     dl,80H-2
  99. SMB1:   int     13H
  100.         mov     ax,201H                 ;duplicate read
  101.         int     13H                     ;in case disk change
  102.         jc      SMBR                    ;exit if error
  103.  
  104.         mov     cx,512                  ;size of data to scan
  105.         mov     ah,MBR_FLAG and 255     ;scan for boot sector viruses
  106.         call    SCAN_DATA               ;go scan the data
  107.  
  108. SMBR:   ret
  109.  
  110.  
  111. ;This routine scans the boot sector for both floppy disks and hard disks.
  112. ;For hard disks, the master boot sector must be in the data buffer when
  113. ;this is called, so it can find the boot sector.
  114. SCAN_BOOT:
  115.         mov     WORD PTR [FILE_NAME],OFFSET BOOT_NAME
  116.         mov     cx,1                    ;assume floppy parameters
  117.         mov     dh,0
  118.         mov     dl,[DISK_DR]
  119.         cmp     BYTE PTR [DISK_DR],2
  120.         jc      SB2                     ;go handle floppies if so
  121.  
  122.         mov     si,OFFSET DATA_BUF + 1BEH
  123. SBL:    cmp     BYTE PTR [si],80H       ;check active flag
  124.         je      SB1                     ;active, go get it
  125.         add     si,10H                  ;else try next partition
  126.         cmp     si,1FEH                 ;at the end of table?
  127.         jne     SBL                     ;no, do another
  128.         ret                             ;yes, no active partition, just exit
  129.  
  130. SB1:    mov     dx,[si]                 ;set up dx and cx for read
  131.         mov     cx,[si+2]
  132. SB2:    mov     bx,OFFSET DATA_BUF
  133.         push    ds
  134.         pop     es
  135.         mov     ax,201H
  136.         int     13H                     ;read boot sector
  137.  
  138.         mov     cx,512
  139.         mov     ah,BOOT_FLAG
  140.         call    SCAN_DATA               ;and scan it
  141.         ret
  142.  
  143. ;This routine systematically scans all RAM below 1 Meg for resident viruses.
  144. ;If a virus is found, it returns with c set. Otherwise c is reset.
  145. SCAN_RAM:
  146.         mov     WORD PTR [FILE_NAME],OFFSET RAM_NAME
  147.         xor     ax,ax
  148.         mov     es,ax
  149.         mov     bx,ax                   ;set es:bx=0
  150. SRL:    mov     ah,RAM_FLAG             ;prep for scan
  151.         mov     cx,8010H                ;scan this much in a chunk
  152.         call    SCAN_DATA               ;scan ram
  153.         pushf
  154.         mov     ax,es                   ;update es for next chunk
  155.         add     ax,800H
  156.         mov     es,ax
  157.         popf
  158.         jc      SREX                    ;exit if a virus was found
  159.         or      ax,ax                   ;are we done?
  160.         jnz     SRL                     ;nope, get another chunk
  161.         clc                             ;no viruses, return nc
  162. SREX:   ret
  163.  
  164. ;This routine scans all EXEs and COMs on the current disk looking for viruses.
  165. ;This routine is fully recursive.
  166. SCAN_ALL_FILES:
  167.         push    bp                      ;build stack frame
  168.         mov     bp,sp
  169.         sub     bp,43                   ;space for file search record
  170.         mov     sp,bp
  171.  
  172.         mov     dx,OFFSET SEARCH_REC    ;set up DTA
  173.         mov     ah,1AH
  174.         int     21H
  175.  
  176.         call    SCAN_COMS               ;scan COM files in current directory
  177.         call    SCAN_EXES               ;scan EXE files in current directory
  178.  
  179.         mov     dx,bp                   ;move DTA for directory search
  180.         mov     ah,1AH                  ;this part must be recursive
  181.         int     21H
  182.  
  183.         mov     dx,OFFSET ANY_FILE
  184.         mov     ah,4EH                  ;prepare for search first
  185.         mov     cx,10H                  ;dir file attribute
  186.         int     21H                     ;do it
  187.  
  188. SAFLP:  or      al,al                   ;done yet?
  189.         jnz     SAFEX                   ;yes, quit
  190.         cmp     BYTE PTR [bp+30],'.'
  191.         je      SAF1                    ;don't mess with fake subdirectories
  192.         test    BYTE PTR [bp+21],10H
  193.         jz      SAF1                    ;don't mess with non-directories
  194.         lea     dx,[bp+30]
  195.         mov     ah,3BH                  ;go into subdirectory
  196.         int     21H
  197.  
  198.         call    UPDATE_PATH             ;update the PATH viariable
  199.         push    ax                      ;save end of original PATH
  200.  
  201.         call    SCAN_ALL_FILES          ;search all files in the subdirectory
  202.  
  203.         pop     bx
  204.         mov     BYTE PTR [bx],0         ;truncate PATH variable to original
  205.  
  206.         mov     dx,bp                   ;restore DTA, continue dir search
  207.         mov     ah,1AH
  208.         int     21H
  209.  
  210.         mov     dx,OFFSET UP_ONE        ;go back to this directory
  211.         mov     ah,3BH
  212.         int     21H
  213. SAF1:   mov     ah,4FH                  ;search next
  214.         int     21H
  215.         jmp     SAFLP                   ;and continue
  216.  
  217. SAFEX:  add     bp,43
  218.         mov     sp,bp
  219.         pop     bp                      ;restore stack frame and exit
  220.         ret
  221.  
  222.  
  223. ;This routine scans all EXE files in the current directory looking for viruses.
  224. SCAN_EXES:
  225.         mov     BYTE PTR [FFLAGS],EXE_FLAG and 255
  226.         mov     WORD PTR [FILE_NAME],OFFSET SEARCH_REC + 30 ;where file name ends up
  227.  
  228.         mov     dx,OFFSET EXE_FILE
  229.         jmp     SCAN_FILES
  230.  
  231. ;This routine scans all COM files in the current directory looking for viruses.
  232. SCAN_COMS:
  233.         mov     BYTE PTR [FFLAGS],COM_FLAG
  234.         mov     WORD PTR [FILE_NAME],OFFSET SEARCH_REC + 30 ;where file name ends up
  235.  
  236.         mov     dx,OFFSET COM_FILE
  237. SCAN_FILES:
  238.         mov     ah,4EH                  ;prepare for search first
  239.         mov     cx,3FH                  ;any file attribute
  240.         int     21H                     ;do it
  241.  
  242. SCLP:   or      al,al                   ;an error?
  243.         jnz     SCDONE                  ;if so, we're done
  244.         call    SCAN_FILE               ;scan the file
  245.         mov     ah,4FH                  ;search for next file
  246.         int     21H
  247.         jmp     SCLP                    ;and go check it
  248.  
  249. SCDONE: ret                             ;all done, exit
  250.  
  251. ;This routine scans a single file for viruses. The @ of the file name is assumed
  252. ;to be at ds:[FILE_NAME]. The flags to use in the scan are at ds:[FFLAGS]
  253. SCAN_FILE:
  254.         mov     dx,WORD PTR [FILE_NAME]
  255.         mov     ax,3D00H                ;open file
  256.         int     21H
  257.         jc      SFCLOSE                 ;exit if we can't open it
  258.         mov     bx,ax
  259. SF1:
  260.         mov     ah,3FH                  ;read file
  261.         mov     cx,DBUF_SIZE
  262.         mov     dx,OFFSET DATA_BUF
  263.         int     21H
  264.         cmp     ax,16                   ;did we actually read anything?
  265.         jle     SFCLOSE                 ;nope, done, go close file
  266.  
  267.         mov     cx,ax                   ;size of data read to cx
  268.         push    bx                      ;save file handle
  269.         mov     bx,OFFSET DATA_BUF
  270.         push    ds
  271.         pop     es
  272.         mov     ah,[FFLAGS]
  273.         call    SCAN_DATA
  274.         pop     bx                      ;restore file handle
  275.         jc      SFCL2                   ;if a virus found, exit with c set
  276.  
  277.         mov     ax,4201H                ;move file pointer relative to current
  278.         mov     cx,-1                   ;back 16 bytes
  279.         mov     dx,-16                  ;so we don't miss a virus at the
  280.         int     21H                     ;buffer boundary
  281.         jmp     SF1
  282.  
  283. SFCLOSE:clc                             ;exit when no virus found, c reset
  284. SFCL2:  pushf                           ;save flags temporarily
  285.         mov     ah,3EH                  ;close file
  286.         int     21H
  287.         popf
  288.  
  289.         ret
  290.  
  291. ;This routine scans data at es:bx for viruses. The amount of data to
  292. ;scan is put in cx, and the flag mask to examine is put in ah. SCAN_DATA
  293. ;will return with c set if a scan string was found, and nc if not.
  294. SCAN_DATA:
  295.         mov     WORD PTR [DSIZE],cx
  296.         mov     si,OFFSET SCAN_STRINGS  ;si is an index into the scan strings
  297. SD1:    lodsb                           ;get flag byte
  298.         push    ax
  299.         and     al,END_OF_LIST          ;end of list?
  300.         pop     ax
  301.         jnz     SDR                     ;yes, exit now
  302.         and     al,ah                   ;no, so is it a string of proper type?
  303.         jz      SDNEXT                  ;no, go do next string
  304.  
  305.         mov     dx,bx
  306.         add     dx,[DSIZE]              ;dx = end of search buffer
  307.         mov     di,bx                   ;di = start of search buffer
  308. SD2:    mov     al,[si]                 ;get 1st byte of string
  309.         xor     al,0AAH
  310.         cmp     di,dx                   ;end of buffer yet?
  311.         je      SDNEXT                  ;yes, go do next string
  312.         cmp     al,es:[di]              ;compare with byte of buffer
  313.         je      SD3                     ;equal, go check rest of string
  314.         inc     di                      ;else check next byte in buffer
  315.         jmp     SD2
  316.  
  317. SD3:    push    si                      ;check for entire 16 byte string
  318.         push    di                      ;at es:di
  319.         mov     cx,16
  320. SD4:    lodsb                           ;ok, do it
  321.         xor     al,0AAH                 ;decrypt
  322.         inc     di
  323.         cmp     al,es:[di-1]
  324.         loopz   SD4
  325.  
  326.         pop     di
  327.         pop     si
  328.         pushf
  329.         inc     di
  330.         popf
  331.         jne     SD2                     ;not equal, go try next byte
  332.         mov     di,si                   ;else calculate the index for this
  333.         sub     di,OFFSET SCAN_STRINGS+1;virus to display its name on screen
  334.         mov     ax,di
  335.         mov     di,17
  336.         xor     dx,dx
  337.         div     di
  338.         mov     di,ax
  339.         call    DISP_VIR_NAME           ;go display its name
  340.         stc                             ;set carry
  341.         ret                             ;and exit
  342.  
  343. SDNEXT: add     si,16                   ;go to next scan string
  344.         jmp     SD1
  345.  
  346. SDR:    clc                             ;clear carry, no virus found
  347.         ret                             ;and exit
  348.  
  349. ;This routine updates the variable PATH to reflect a new directory. It also
  350. ;returns a pointer to the end of the old path in ax. It is used only in
  351. ;conjunction with SCAN_ALL_FILES.
  352. UPDATE_PATH:
  353.         lea     di,[bp+30]              ;update PATH variable
  354.         mov     si,OFFSET PATH
  355. SAF01:  lodsb                           ;find end of existing PATH
  356.         or      al,al
  357.         jnz     SAF01
  358.         dec     si
  359.         mov     dx,si                   ;save end here
  360.         push    cs
  361.         pop     es
  362.         xchg    si,di
  363. SAF02:  lodsb                           ;move new directory to PATH
  364.         stosb
  365.         or      al,al
  366.         jnz     SAF02
  367.         dec     di
  368.         mov     ax,'\'                  ;terminate path with backslash
  369.         stosw
  370.         mov     ax,dx
  371.         ret
  372.  
  373. ;This routine displays the virus name indexed by di. If di=0 then this
  374. ;displays the first ASCIIZ string at NAME_STRINGS, if di=1 then it displays
  375. ;the second, etc.
  376. DISP_VIR_NAME:
  377.         mov     si,OFFSET PATH
  378. FV00:   lodsb
  379.         or      al,al
  380.         jz      FV01
  381.         mov     ah,0EH
  382.         int     10H
  383.         jmp     FV00
  384.  
  385. FV01:   mov     si,[FILE_NAME]
  386. FV02:   lodsb
  387.         or      al,al
  388.         jz      FV05
  389.         mov     ah,0EH
  390.         int     10H
  391.         jmp     FV02
  392.  
  393. FV05:   mov     si,OFFSET NAME_STRINGS
  394. FV1:    or      di,di
  395.         jz      DISP_NAME
  396.         push    di
  397. FV2:    lodsb
  398.         cmp     al,'$'
  399.         jnz     FV2
  400.         pop     di
  401.         dec     di
  402.         jmp     FV1
  403.  
  404. DISP_NAME:
  405.         push    si
  406.         mov     dx,OFFSET INFECTED
  407.         mov     ah,9
  408.         int     21H
  409.         pop     dx
  410.         mov     ah,9
  411.         int     21H
  412.         mov     dx,OFFSET VIRUS_ST
  413.         mov     ah,9
  414.         int     21H
  415.         ret
  416.  
  417. HELLO           DB      'GB-SCAN Virus Scanner Ver. 1.00 (C) 1995 American Eagle Publications Inc.',0DH,0AH,24H
  418. INFECTED        DB      ' is infected by the $'
  419. VIRUS_ST        DB      ' virus.',0DH,0AH,24H
  420. MBR_NAME        DB      'The Master Boot Record',0
  421. BOOT_NAME       DB      'The Boot Sector',0
  422. RAM_NAME        DB      7,7,7,7,7,'ACTIVE MEMORY',0
  423. EXE_FILE        DB      '*.EXE',0
  424. COM_FILE        DB      '*.COM',0
  425. ANY_FILE        DB      '*.*',0
  426. ROOT            DB      '\',0
  427. UP_ONE          DB      '..',0
  428.  
  429. SCAN_STRINGS    DB      (COM_FLAG or EXE_FLAG) and 255    ;MINI-44 virus
  430.                 DB      1EH,0E4H,10H,8CH,0ABH,67H,8BH,0D8H,0B6H,12H,0ABH,97H,10H,34H,0AAH,67H
  431.                 DB      BOOT_FLAG               ;Kilroy-B virus (live)
  432.                 DB      12H,0ABH,0A8H,11H,0AAH,0AFH,13H,0ABH,0AAH,10H,0ABH,0AAH,67H,0B9H,12H,0ABH
  433.                 DB      COM_FLAG                ;Kilroy-B virus (dropper)
  434.                 DB      12H,0ABH,0A8H,11H,0AAH,0AFH,13H,0ABH,0AAH,10H,0ABH,0AAH,67H,0B9H,12H,0ABH
  435.                 DB      (EXE_FLAG or RAM_FLAG) and 255    ;The Yellow Worm
  436.                 DB      0FAH,0A4H,0B5H,26H,0ACH,86H,0AAH,12H,0AAH,0BCH,67H,85H,8EH,0D5H,96H,0AAH
  437.                 DB      END_OF_LIST             ;end of scan string list
  438.  
  439. NAME_STRINGS    DB      'MINI-44$'
  440.                 DB      'Kilroy-B$'
  441.                 DB      'Kilroy-B dropper$'
  442.                 DB      'Yellow Worm$'
  443.  
  444. PATH            DB      80 dup (?)
  445. CURR_DIR        DB      64 dup (?)
  446. DSIZE           DW      ?
  447. SEARCH_REC      DB      43 dup (?)
  448. CURR_DR         DB      ?                               ;current disk drive
  449. DISK_DR         DB      ?                               ;drive to scan
  450. FFLAGS          DB      ?                               ;flags to use in scan
  451. FILE_NAME       DW      ?                               ;address of file name in memory
  452. DATA_BUF        DB      DBUF_SIZE dup (?)
  453.  
  454.         END     GBSCAN
  455.